<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
//header('Content-type: application/json');
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');

include_once("file.common.php");

$act = req('act', '');

$result = TRUE;
$msg = '';
$log = array();

if ($devserver && $act == "test")
	$notactiveId = "test";

if ($act && $notactiveId && $act == $notactiveId)
{
	// Create the passcode file
	$credfile = "<?PHP\n";
	$credfile .= '$passcode = ' . json_encode($newPasscode) . ";\n";
	$credfile .= '$usercode = ' . json_encode($usercode) . ";\n";
	$credfile .= '$curateEmail = ' . json_encode($curateEmail) . ";\n";
	$credfile .= '$createTime = ' . json_encode($createTime) . ";\n";
	$credfile .= '$srcPasscode = ' . json_encode($srcPasscode) . ";\n";
	$credfile .= "?>\n";
	file_put_contents("curator.php", $credfile);
}
else if ($usercode && !$notactiveId)
{
	$result = TRUE;
	$msg = "Already activated";
}
else
{
	$result = FALSE;
	$msg = "Incorrect activation";
}

if ($act == "test")
{
	autoheader();
	if ($callback)
		$ee = "$callback({\n";
	else
		$ee = "{\n";
	$ee .= '"result":' . json_encode($result) . ",\n";
	$ee .= '"act":' . json_encode($act) . ",\n";
	$ee .= '"email":' . json_encode($curateEmail) . ",\n";
	//$ee .= '"notactiveId":' . json_encode($notactiveId) . ",\n";
	//$ee .= '"password":' . json_encode($password) . ",\n";
	//$ee .= '"passcode":' . json_encode($passcode) . ",\n";
	$ee .= '"msg":' . json_encode($msg) . ",\n";
	$ee .= '"log":' . json_encode($log) . ",\n";
	$ee .= '"end":true' . "\n";
	if ($callback)
		$ee .= "})";
	else
		$ee .= "}";
	echo $ee;
	exit(0);
}

$spaceUrl = "../master.html";

if ($result === TRUE)
{
$ot = <<<EOD
<html>
<head>
<title>Relink to PlaySpace</title>
</head>
<body>
<script type="text/javascript">location.href = "$spaceUrl";</script>
Go to your space <a href="$spaceUrl">$spaceUrl</a>
</body>
</html>
EOD;
} else {
$ot = <<<EOD
<html>
<head>
<title>Invalid activation</title>
</head>
<body>
<h2>Activation code invalid.</h2>
</body>
</html>
EOD;
}

echo $ot;
?>
